<?php /** @noinspection ALL */

//用户管理

namespace app\admin\controller;


use app\middleware\Check;
use think\facade\Db;


class User {

	static function random($length, $numeric = FALSE) {
		$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
		$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
		if ($numeric) {
			$hash = '';
		} else {
			$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
			$length--;
		}
		$max = strlen($seed) - 1;
		for ($i = 0; $i < $length; $i++) {
			$hash .= $seed{mt_rand(0, $max)};
		}
		return $hash;
	}

	//编辑用户需要的代理列表
	function need_agentList_for_edit_user(){
		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		if(empty($id))return json(['code' => 0,'msg'=>'用户参数错误']);

		if($type==2){//代理
			$agent_list = Db::query("select id,level,nick_name from agent where status =1 and find_in_set('{$id}',team_tag) and level <= 3 ");
		}else{
			$agent_list=Db::table('agent')->field('id,level,nick_name')->where('status', 1)->select()->all();
		}
		return json(['code' => 1, 'data' => $agent_list]);
	}

	//提交 添加用户 && 编辑用户 数据
	function create_user() {
		$input = input();

		if(!empty($input['id'])){
			$userinfo=Db::table('system_user')->where('id', $input['id'])->find();

			if(empty($userinfo)) return json(['code' => 0, 'msg' => '用户不存在']);
		}

		if (empty($input['number'])) return json(['code' => 0, 'msg' => '请输入账号']);

		if (empty($input['password'])) return json(['code' => 0, 'msg' => '请输入密码']);

		if (empty($input['agent_id'])) return json(['code' => 0, 'msg' => '请选择代理']);

		if ($input['status']=='') return json(['code' => 0, 'msg' => '请选择状态']);

		$is_exists=Db::table('system_user')->where('number', trim($input['number']))->find();

		if(empty($input['id'])){

			$security=self::random(16);

			$password=md5($security.$input['password']);

			if(!empty($is_exists)) return json(['code' => 0, 'msg' => "该账号已存在"]);

			$number=$input['number'];

		}else{
			//判断是否修改密码
			$security=$userinfo['security'];

			if($input['password']==$userinfo['password']){

				$password=$userinfo['password'];
			}else{
				$password=md5($userinfo['security'].$input['password']);
			}

			//判断编码是否重复
			if($userinfo['number']==$input['number']){

				$number=$userinfo['number'];

			}else{

				if(!empty($is_exists)) return json(['code' => 0, 'msg' => "该账号已存在"]);

				$number=$input['number'];
			}

		}

		$data = [
			'nick_name' => empty($input['nick_name'])?'用户'.rand(1000,9999):$input['nick_name'],
			'real_name' => empty($input['real_name'])?'用户'.rand(1000,9999):$input['real_name'],
			'number' => $number,
			'password' => $password,
			'security' => $security,
			'tel' => empty($input['tel'])?'':$input['tel'],
			'agent_id' => $input['agent_id'],
			'status' => $input['status'],
			'avatar' => 'https://dss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3280696440,2906944118&fm=26&gp=0.jpg',
			'add_time' => time(),
			'last_ip' => '',
			'last_time' => '',
			'c_status' => 0,
		];

		if(empty($input['id'])){

			$system_user_id = Db::name('system_user')->insertGetId($data);

			if (!$system_user_id) {

				return json(['code' => 0, 'msg' => '编辑失败']);
			}

			$user_account=[
				'uid'=>$system_user_id,
				'amount'=>0.00,
				'gold'=>100.00,
				'frozen_amount'=>0.00,
				'frozen_gold'=>0.00,
				'total_recharge'=>0.00,
				'total_withdrawal'=>0.00,
				'total_transaction'=>0.00,
				'total_fee'=>0.00,
			];

			Db::name('user_account')->insert($user_account);

		}else{
			$r = Db::name('system_user')->where("id",$input['id'])->update($data);

			if (!$r) {

				return json(['code' => 0, 'msg' => '编辑失败']);
			}
		}

		return json(['code' => 1, 'msg' => '编辑成功']);
	}

	//展示用户列表
	function get_all_userList($page=1,$pageSize=10){

		$input=input();

		$userinfo=Pubilc::check_jwt($input['token']);

		if(!$userinfo)return json(['code' => 0,'msg'=>'请重新登录']);

		$id=$userinfo->userId;

		$type=$userinfo->type;

		if(empty($id))return json(['code' => 0,'msg'=>'用户参数错误']);

		$condition = ' and 1=1';

		if (!empty($input['userinfo'])) {
			$input['userinfo']=trim($input['userinfo']);
			$condition .= " and (u.nick_name like '%{$input['userinfo']}%' or u.tel like '%{$input['userinfo']}%' or u.real_name like '%{$input['userinfo']}%' ) ";
		}

		if (!empty($input['time'])) {
			$start=$input['time'][0]/1000;
			$end=$input['time'][1]/1000;
			$condition .= " and u.add_time >= $start and u.add_time <=  $end";
		}

		if (!empty($input['agentinfo'])) {
			$condition .= " and a.nick_name like '%{$input['agentinfo']}%' ";
		}

		if($type==2){//代理
			$userList = Db::query("select u.*,a.nick_name as a_nick_name from system_user as u join agent as a on a.id = u.agent_id where 1".$condition." and find_in_set('{$id}',a.team_tag) order by u.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num = Db::query("select u.*,a.nick_name as a_nick_name from system_user as u join agent as a on a.id = u.agent_id where 1".$condition." and find_in_set('{$id}',a.team_tag) ");

		}else{//管理员

			$userList = Db::query("select u.*,a.nick_name as a_nick_name from system_user as u join agent as a on a.id = u.agent_id where 1".$condition." order by u.id desc limit ".(($page-1)*$pageSize).",$pageSize");

			$total_num = Db::query("select u.*,a.nick_name as a_nick_name from system_user as u join agent as a on a.id = u.agent_id where 1".$condition);
		}

		for ($i = 0; $i < count($userList); $i++) {

			$userList[$i]['add_time']=date('Y-m-d H:i:s',$userList[$i]['add_time']);

		}

		return json(['code' => 1, 'data' => $userList,'total_num'=>count($total_num)]);
	}

	//删除用户
	function del_user() {
		$input = input();
		if (!$input['id']) return json(['code' => 0, 'msg' => '数据错误']);
		$user = Db::table('system_user')->where('id', $input['id'])->find();
		if (empty($user)) return json(['code' => 0, 'msg' => '角色不存在']);
		$re = Db::name('system_user')->where('id', $input['id'])->delete();
		if (!$re) {
			return json(['code' => 0, 'msg' => '删除失败']);
		}
		return json(['code' => 1, 'msg' => '删除成功']);
	}


	//展示用户银行卡
	function get_user_bankList($page=1,$pageSize=10){
		$input=input();

		$condition = ' and 1=1';

		if (!empty($input['userinfo'])) {
			$condition .= " and (su.nick_name like '%{$input['userinfo']}%' or su.tel like '%{$input['userinfo']}%' or su.real_name like '%{$input['userinfo']}%' ) ";
		}

		if (!empty($input['time'])) {
			$start=$input['time'][0]/1000;
			$end=$input['time'][1]/1000;
			$condition .= " and ub.add_time >= $start and ub.add_time <=  $end";
		}

		$user_bankList = Db::query("select b.name as bank_name,ub.*,su.nick_name,su.tel,su.real_name,FROM_UNIXTIME(ub.add_time,'%Y-%m-%d %H:%i:%s')  as add_time from user_bank as ub join bank as b on ub.bank_id = b.id join system_user as su on su.id=ub.uid where 1".$condition." order by ub.id desc limit ".(($page-1)*$pageSize).",$pageSize");

		$total_num = Db::query("select * from user_bank as ub join bank as b on ub.bank_id = b.id join system_user as su on su.id=ub.uid where 1".$condition." order by ub.id desc");

		return json(['code' => 1, 'data' => $user_bankList,'total_num'=>count($total_num)]);
	}



}